﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Web.Script.Serialization;

namespace Virus.Game
{
    public class Virus
    {
        /// <summary>
        /// 地图ID，为今后扩展用
        /// </summary>
        public int Map;

        static List<point> points = new List<point>();

        public Virus()
        {
         //   maxPoint = new point(Webgame.Config.MapConfig.MaxWidth, Webgame.Config.MapConfig.MaxHeight);
        }

        static Virus()
        {
            points.Add(new point(0, 1));
            points.Add(new point(1, 0));
            points.Add(new point(0, -1));
            points.Add(new point(-1, 0));

            points.Add(new point(1, 1));
            points.Add(new point(1, -1));
            points.Add(new point(-1, -1));
            points.Add(new point(-1, 1));
        }

        /// <summary>
        /// 获得当前病毒的数量
        /// </summary>
        /// <returns></returns>
        public int GetVirusNum()
        {
            return VirusMap.Count;
        }

        /// <summary>
        /// 当前病毒在地图里的位置
        /// </summary>
        Dictionary<int, Webgame.Entity.VirusStatus> VirusMap = new Dictionary<int, Webgame.Entity.VirusStatus>();

        #region 对象缓存

        /// <summary>
        /// 玩家得分Cache对象
        /// </summary>
        Dictionary<int, Webgame.Entity.PlayerScore> playerScoreCache = new Dictionary<int, Webgame.Entity.PlayerScore>();

        /// <summary>
        /// 获得玩家对象，这里通过一个字典对玩家对象进行缓存
        /// </summary>
        /// <param name="playerID"></param>
        /// <returns></returns>
        internal Webgame.Entity.PlayerScore GetPlayerScore(int playerID)
        {
            Webgame.Entity.PlayerScore player = null;
            if (playerScoreCache.TryGetValue(playerID, out player))
                return player;

            player = Webgame.Access.PlayerScore.Instance.GetPlayerScoreByPlayerID(Map, playerID);

            if (player == null)
            {
                player = new Webgame.Entity.PlayerScore();
                player.MapID = Map;
                player.PlayerID = playerID;

                //  病毒第一次投放时，需要先给一个默认病毒的养分
                player.Nutrient = Webgame.Config.MapConfig.CostNutrient * 10;

                Webgame.Access.PlayerScore.Instance.Add(player);
            }

            playerScoreCache.Add(player.ID, player);
            return player;
        }

        static Dictionary<int, Webgame.Entity.Player> playerCache = new Dictionary<int, Webgame.Entity.Player>();

        /// <summary>
        /// 获得玩家实体
        /// 玩家实体对象是所有地图共有的，因此放到静态字典里
        /// </summary>
        /// <param name="playerID"></param>
        /// <returns></returns>
        internal Webgame.Entity.Player GetPlayer(int playerID)
        {
            Webgame.Entity.Player player;
            if (playerCache.TryGetValue(playerID, out player))
                return player;

            player = Webgame.Access.Player.Instance.GetPlayerByID(playerID);
            if (player != null)
                playerCache.Add(player.ID, player);
            return player;
        }

        /// <summary>
        /// 删除玩家对象的缓存，主要是当玩家颜色更新后，需要让其他玩家看到新的颜色
        /// </summary>
        /// <param name="playerID"></param>
        static internal void DeletePlayerCache(int playerID)
        {
            playerCache.Remove(playerID);
        }

        #endregion

        /// <summary>
        /// 处理病毒的分裂
        /// </summary>
        public void Do()
        {
            lock (this)
            {
                bool hasChange = false;
                while (DateTime.Now > nextDoTime)
                {
                    Do(0);
                    hasChange = true;
                }

                if (hasChange)
                    this.Save();
            }
        }

        /// <summary>
        /// 处理病毒的分裂
        /// </summary>
        public void Do(int round)
        {
            Dictionary<int, Webgame.Entity.VirusStatus> map = new Dictionary<int, Webgame.Entity.VirusStatus>();
            Dictionary<int, List<Webgame.Entity.VirusStatus>> map2 = new Dictionary<int, List<Webgame.Entity.VirusStatus>>();

            //  当前地图里存在的玩家
            Dictionary<int, Webgame.Entity.PlayerScore> currentPlayerMap = new Dictionary<int, Webgame.Entity.PlayerScore>();

            #region 产生新的病毒

            int newRound = round  + 1;
            foreach (Webgame.Entity.VirusStatus vs in VirusMap.Values)
            {
                if (!currentPlayerMap.ContainsKey(vs.pID))
                    currentPlayerMap.Add(vs.pID, GetPlayerScore(vs.pID));

                #region 如果权重小于5则不进行分裂处理

                if (vs.Width < 5)
                {
                    vs.Width += 5;
                    int key = GetKey(vs.X, vs.Y);
                    if (!map.ContainsKey(key))
                    {
                        map.Add(key, vs);
                    }
                    else
                    {
                        if (map2.ContainsKey(key))
                        {
                            map2[key].Add(vs);
                        }
                        else
                        {
                            List<Webgame.Entity.VirusStatus> nvs = new List<Webgame.Entity.VirusStatus>();
                            nvs.Add(map[key]);
                            nvs.Add(vs);

                            map2.Add(key, nvs);
                        }
                    }

                    continue;
                }

                #endregion

                int newPoint = 4;

                if (vs.Status != null)
                {
                    if (vs.Status.Status == Webgame.Entity.emVirusStatus.超级分裂)
                    {
                        newPoint = 8;
                    }
                    else if (vs.Status.Status == Webgame.Entity.emVirusStatus.无敌 &&  vs.Status.LessTime > 0)
                    {
                        vs.Status.LessTime--;
                        int key = GetKey(vs.X, vs.Y);

                        if (!map.ContainsKey(key))
                        {
                            map.Add(key, vs);
                        }
                        else
                        {
                            if (map2.ContainsKey(key))
                            {
                                map2[key].Add(vs);
                            }
                            else
                            {
                                List<Webgame.Entity.VirusStatus> nvs = new List<Webgame.Entity.VirusStatus>();
                                nvs.Add(map[key]);
                                nvs.Add(vs);

                                map2.Add(key, nvs);
                            }
                        }
                    }
                }

                for (int i = 0; i < newPoint; i++)
                {
                    Webgame.Entity.VirusStatus newVS = new Webgame.Entity.VirusStatus();
                    newVS.X = points[i].x + vs.X;
                    newVS.Y = points[i].y + vs.Y;

                    if (newVS.X < -1 || newVS.X >= maxPoint.x)
                        continue;
                    if (newVS.Y < -1 || newVS.Y >= maxPoint.y)
                        continue;

                    newVS.Round = newRound;
                    newVS.pID = vs.pID;
                    newVS.Width = vs.Width <= 5 ? 5 : vs.Width - 1;

                    int key = GetKey(newVS.X, newVS.Y);

                    if (!map.ContainsKey(key))
                    {
                        map.Add(key, newVS);
                    }
                    else
                    {
                        if (map2.ContainsKey(key))
                        {
                            map2[key].Add(newVS);
                        }
                        else
                        {
                            List<Webgame.Entity.VirusStatus> nvs = new List<Webgame.Entity.VirusStatus>();
                            nvs.Add(map[key]);
                            nvs.Add(newVS);

                            map2.Add(key, nvs);
                        }
                    }
                    
                }
            }

            #endregion

            #region 处理同一格子里存在多个病毒

            foreach (int key in map2.Keys)
                map.Remove(key);

            foreach (List<Webgame.Entity.VirusStatus> nvs in map2.Values)
            {
                SortedList<int, int> counts = new SortedList<int, int>();
                
                Webgame.Entity.VirusStatus newVS;
                foreach(Webgame.Entity.VirusStatus vs in nvs)
                {
                    if (vs.Status != null && vs.Status.Status == Webgame.Entity.emVirusStatus.无敌)
                    {
                        map.Add(GetKey(vs.X, vs.Y), vs);
                        //  取消下面的switch流程的判断
                        counts.Clear();
                        break;
                    }

                    int num;
                    if (counts.TryGetValue(vs.pID, out num))
                        counts[vs.pID] = num + vs.Width;
                    else
                        counts.Add(vs.pID, vs.Width);
                }

                switch (counts.Count)
                {
                    case 1:
                        //  如果叠加起来只有一个病毒，并且这个病毒的权重小于10，则保留
                        //  不过貌似不存在这样的情况
                        int width = counts.Values[0];
                        if (width < 10)
                        {
                            newVS = nvs[0];
                            newVS.Width = width;
                            map.Add(GetKey(newVS.X, newVS.Y), newVS);
                        }
                        break;
                    case 2:
                        //  一个区域里存在2个病毒，比较两个病毒，并保留权重最大的那个
                        //  权重最大的剩余 = 最大权重 - 最小权重
                        int width1 = counts.Values[0];
                        int width2 = counts.Values[1];

                        if (width1 == width2)
                            continue;
                        int maxWidth = Math.Max(counts.Values[0], counts.Values[1]);
                        int pid = counts.Keys[counts.IndexOfValue(maxWidth)];
                        
                        newVS = nvs[0].pID == pid ? nvs[0] : nvs[1];
                        newVS.Width = Math.Abs(width1 - width2);
                        // 一个点的病毒，某位玩家有超过2个以上病毒到达，就有可能产生10以上的权重
                        // 那么，上面的取差的绝对值就会大于10，与游戏预先设想的病毒的权不能超过10不一致
                        if (newVS.Width > 10)
                            newVS.Width = 10;

                        map.Add(GetKey(newVS.X, newVS.Y), newVS);
                        currentPlayerMap[newVS.pID].Eat++;
                        break;
                    case 3:
                        //  存在三个病毒，方法同上，剩余的病毒为最大值和第二大的差
                        int maxIndex = counts.Values[0] > counts.Values[1] ? 0 : 1;
                        int secendIndex = 1 - maxIndex;
                        if (counts.Values[maxIndex] < counts.Values[2])
                        {
                            secendIndex = maxIndex;
                            maxIndex = 2;
                        }
                        else
                        {
                            if (counts.Values[secendIndex] < counts.Values[2])
                                secendIndex = 2;
                        }

                        newVS = nvs[counts.Keys[maxIndex]];
                        newVS.Width = Math.Abs(counts.Values[maxIndex] - counts.Values[maxIndex]);

                        if (newVS.Width > 10)
                            newVS.Width = 10;

                        map.Add(GetKey(newVS.X, newVS.Y), newVS);
                        currentPlayerMap[newVS.pID].Eat++;

                        break;
                }
            }

            VirusMap = map;

            #endregion

            #region 计算本次病毒获得的养分，每分裂一个病毒获得1点养分

            //  将当前病毒数量清零
            foreach (KeyValuePair<int, Webgame.Entity.PlayerScore> p in currentPlayerMap)
                p.Value.Virus = 0;

            foreach (System.Collections.Generic.KeyValuePair<int, Webgame.Entity.VirusStatus> entry in map)
            {
                Webgame.Entity.PlayerScore p = GetPlayerScore(entry.Value.pID);
                p.Nutrient++;
                p.Virus++;
            }

            #endregion

            #region 将本回合的得分写入数据库

            Webgame.Access.PlayerScore ps = Webgame.Access.PlayerScore.Instance;
            foreach (KeyValuePair<int, Webgame.Entity.PlayerScore> score in currentPlayerMap)
            {
                if (score.Value.Nutrient > Webgame.Config.MapConfig.MaxNutrient)
                    score.Value.Nutrient = Webgame.Config.MapConfig.MaxNutrient;

                ps.Update(score.Value);
            }

            #endregion

            nextDoTime = nextDoTime.AddSeconds(this.GameTimeSpan);

            #region 临时增加，用于GC回收，测试一下对虚拟主机会不会有影响

            map2.Clear();
            currentPlayerMap.Clear();
            System.GC.Collect();

            #endregion
        }

        /// <summary>
        /// 获得当前点周边范围病毒的信息
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public List<Webgame.Entity.VirusStatus> GetVirus(int x, int y, int round)
        {
            x = ReSetPointX(x);

            y = ReSetPointY(y);

            List<Webgame.Entity.VirusStatus> ret = new List<Webgame.Entity.VirusStatus>();
            for( int xx = x - Page ; xx < x + Page; xx++)
                for (int yy = y - Page; yy < y + Page; yy++)
                {
                    Webgame.Entity.VirusStatus vs = null;
                    if (VirusMap.TryGetValue(GetKey(xx, yy), out vs))
                        ret.Add(vs);
                }

            return ret;
        }

        internal int ReSetPointY(int y)
        {
            if (y < Page)
                y = Page;
            if (y > maxPoint.y - Page)
                y = maxPoint.y - Page;
            return y;
        }

        internal int ReSetPointX(int x)
        {
            if (x < Page)
                x = Page;
            if (x > maxPoint.x - Page)
                x = maxPoint.x - Page;
            return x;
        }

        /// <summary>
        /// 获得当前点的病毒
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public Webgame.Entity.VirusStatus GetVirus(int x, int y)
        {
            int key = GetKey(x, y);
            Webgame.Entity.VirusStatus ret = null;
            if (VirusMap.TryGetValue(key, out ret))
                return ret;
            return null;
        }

        /// <summary>
        /// 投放一个新的病毒
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="playerID"></param>
        /// <returns></returns>
        public bool AddVirus(int x, int y, int playerID)
        {
            if (x > -1 && x < maxPoint.x && y > -1 && y < maxPoint.y)
            {
                Webgame.Entity.VirusStatus vs;

                if (VirusMap.TryGetValue(GetKey(x, y), out vs))
                    return false;

                Webgame.Entity.PlayerScore ps = GetPlayerScore(playerID);

                //  如果没有足够的养分则不能进行病毒的分裂
                if (ps.Nutrient < Webgame.Config.MapConfig.CostNutrient)
                    return false;

                ps.Nutrient -= Webgame.Config.MapConfig.CostNutrient;

                //  数据更新后立即会写数据库
                Webgame.Access.PlayerScore.Instance.Update(ps);

                vs = new Webgame.Entity.VirusStatus();
                vs.pID = playerID;
                vs.X = x;
                vs.Y = y;
                vs.Width = 5;
                VirusMap.Add(GetKey(x, y), vs);

                return true;
            }

            return false;
        }

        private int page = 10;

        public int Page
        {
            get { return page; }
        }

        point maxPoint;

        private point MaxPoint
        {
            get { return maxPoint; }
            set { maxPoint = value; }
        }

        static public int GetKey(int x, int y)
        {
            return x * maxStart + y;
        }

        static int maxStart = 10000;

        struct point
        {
            public point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }

            public int x;

            public int y;
        }

        /// <summary>
        /// 重置整个地图
        /// </summary>
        public void ReSet()
        {
            //  清除地图上玩家的信息
            VirusMap.Clear();
            
            //  清除缓存里玩家得分信息
            playerScoreCache.Clear();
            playerCache.Clear();

            //  重新设置开始时间
            nextDoTime = DateTime.Now.AddSeconds(-1);
            Do();
        }

        /// <summary>
        /// 一局游戏结束
        /// </summary>
        public void EndRoundGame()
        {
            lock (this)
            {
                Dictionary<int, Webgame.Entity.PlayerScore> map = new Dictionary<int, Webgame.Entity.PlayerScore>();
                foreach (Webgame.Entity.VirusStatus v in VirusMap.Values)
                {
                    Webgame.Entity.PlayerScore ps = GetPlayerScore(v.pID);
                    ps.Nutrient++;
                    if (!map.ContainsKey(ps.ID))
                        map.Add(ps.ID, ps);
                }

                foreach (Webgame.Entity.PlayerScore ps in map.Values)
                {
                    if (ps.Nutrient > Webgame.Config.MapConfig.MaxNutrient)
                        ps.Nutrient = Webgame.Config.MapConfig.MaxNutrient;

                    Webgame.Access.PlayerScore.Instance.Update(ps);
                }

                ReSet();
            }
        }

        /// <summary>
        /// 统计某个时刻的病毒的情况
        /// </summary>
        /// <param name="time">统计时间</param>
        public void TotalGame(DateTime time)
        {
            lock (this)
            {
                //  取每天前3名
                List<Webgame.Entity.PlayerScore> playerScores = Webgame.Access.PlayerScore.Instance.GetTopScore(this.Map, 3);

                for(int i = 0; i < playerScores.Count; i++)
                {
                    Webgame.Entity.PlayerScore ps = playerScores[i];

                    Webgame.Entity.Top top = new Webgame.Entity.Top();
                    top.MapID = this.Map;
                    top.PlayerID = ps.PlayerID;
                    top.Rank = i + 1;
                    top.Score = ps.Eat;
                    top.AddTime = time;

                    Webgame.Access.Top.Instance.Add(top);
                }
            }
        }

        public void testTotal()
        {
            TotalGame(DateTime.Now);

        }

        #region 病毒实例

        /// <summary>
        /// 病毒实例
        /// </summary>
        static Dictionary<int, Virus> virusInstance = null;

        protected static Dictionary<int, Virus> VirusInstance
        {
            get
            {
                if (virusInstance == null)
                {
                    virusInstance = new Dictionary<int, Virus>();
                    foreach (Webgame.Entity.Map map in Webgame.Access.Map.Instance.GetMapAll())
                    {
                        Virus v = new Virus(map);
                        v.Load();
                        virusInstance.Add(v.Map, v);
                    }

                }
                return virusInstance;
            }
        }

        public Virus(Webgame.Entity.Map map):this()
        {
            this.Map = map.ID;
            _Width = map.Width;
            _Height = map.Height;
            _GameTimeSpan = map.SpanTime;
            _NextUpdateTime = map.NextUpdateTime;
            maxPoint = new point(_Width, _Height);
        }

        /// <summary>
        /// 获得一个病毒游戏实例
        /// 这里这样规划，是为了今后在同一个服务器里实现多地图同时运营
        /// </summary>
        /// <param name="MapID"></param>
        /// <returns></returns>
        static public Virus GetVirus(int MapID)
        {
            Virus ret = null;
            if (VirusInstance.TryGetValue(MapID, out ret))
                return ret;

            if (VirusInstance.ContainsKey(0))
                return VirusInstance[0];

            ret = new Virus();           
            VirusInstance.Add(0, ret);

            return null;
        }

        static public List<Virus> GetAllVirus()
        {
            List<Virus> ret = new List<Virus>();
            ret.AddRange(VirusInstance.Values);
            return ret;
        }

        #endregion

        #region property

        string _Name = string.Empty;

        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
            }
        }
        int _Width = 0;

        public int Width
        {
            get
            {
                return _Width;
            }
        }
        int _Height = 0;

        public int Height
        {
            get
            {
                return _Height;
            }
        }
        int _GameTimeSpan = 0;

        /// <summary>
        /// 游戏的时间间隔
        /// </summary>
        public int GameTimeSpan
        {
            get
            {
                return _GameTimeSpan;
            }
        }

        DateTime _NextUpdateTime = DateTime.Now;

        /// <summary>
        /// 下一次更新排行榜的时间
        /// </summary>
        public DateTime NextUpdateTime
        {
            get
            {
                return _NextUpdateTime;
            }
        }
        #endregion

        private DateTime nextDoTime = DateTime.Now;

        /// <summary>
        /// 下一次进行病毒分裂的时间
        /// </summary>
        public DateTime NextDoTime
        {
            get { return nextDoTime; }
        }

        #region 状态保存/恢复

        /// <summary>
        /// 将当前数据进行保存
        /// </summary>
        /// <param name="file"></param>
        public void Save( string file)
        {
            List<Webgame.Entity.VirusStatus> vs = new List<Webgame.Entity.VirusStatus>();
            vs.AddRange(VirusMap.Values);
            
            try
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(file, FileMode.Create,
                FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, vs);
                stream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);                
            }
            
        }

        public void Save()
        {
            Save(Path.Combine(Webgame.Config.GameConfig.BasePath, string.Format("{0}.save", Map)));

        }

        public void Load()
        {
            Load(Path.Combine(Webgame.Config.GameConfig.BasePath, string.Format("{0}.save", Map)));

        }

        /// <summary>
        /// 从文件里加载
        /// </summary>
        /// <param name="file"></param>
        public void Load(string file)
        {
            try
            {
                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(file, FileMode.Open,
                FileAccess.Read, FileShare.Read);
                List<Webgame.Entity.VirusStatus> vs = (List<Webgame.Entity.VirusStatus>)formatter.Deserialize(stream);
                stream.Close();

                foreach (Webgame.Entity.VirusStatus v in vs)
                {
                    VirusMap.Add(GetKey(v.X, v.Y), v);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        #endregion

        #region 道具对应的方法

        /// <summary>
        /// 扩散一个病毒
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public bool 扩散(int playerID, int x, int y)
        {
            Webgame.Entity.VirusStatus vs = null;

            if (!VirusMap.TryGetValue(GetKey(x, y), out vs))
                return false;

            //  如果要求扩散的病毒不是发出命令的玩家，则不予考虑
            if (vs.pID != playerID)
                return false;

            Webgame.Entity.PlayerScore ps = GetPlayerScore(playerID);
            if (ps.Nutrient < 50)
                return false;   //  没有足够的养分

            ps.Nutrient -= 50;
            Webgame.Access.PlayerScore.Instance.Update(ps);

            for (int i = 0; i < 4; i++)
            {
                Webgame.Entity.VirusStatus newVS = new Webgame.Entity.VirusStatus();
                newVS.X = points[i].x + vs.X;
                newVS.Y = points[i].y + vs.Y;
                newVS.Width = vs.Width <= 5 ? 5 : vs.Width - 1;

                if (newVS.X < -1 || newVS.X >= maxPoint.x)
                    continue;
                if (newVS.Y < -1 || newVS.Y >= maxPoint.y)
                    continue;

                newVS.pID = vs.pID;

                int key = GetKey(newVS.X, newVS.Y);

                Webgame.Entity.VirusStatus current = null;
                if (VirusMap.TryGetValue(key, out current))
                {
                    //  扩散的地方存在病毒，并且对方的权比扩展病毒大，则无法扩散到该点
                    if (current.Width <= newVS.Width)
                        VirusMap[key] = newVS;
                    else
                        current.Width -= newVS.Width;
                }
                else
                {
                    //  扩散的地方不存在病毒
                    VirusMap.Add(key, newVS);
                }
            }

            return true;
        }

        /// <summary>
        /// 强化一个病毒，将它的权提升到8
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool 强心针(int playerID, int x, int y)
        {
            Webgame.Entity.VirusStatus vs = null;

            if (!VirusMap.TryGetValue(GetKey(x, y), out vs))
                return false;

            //  如果要求扩散的病毒不是发出命令的玩家，则不予考虑
            if (vs.pID != playerID)
                return false;

            Webgame.Entity.PlayerScore ps = GetPlayerScore(playerID);
            if (ps.Nutrient < 50)
                return false;   //  没有足够的养分

            ps.Nutrient -= 50;
            Webgame.Access.PlayerScore.Instance.Update(ps);

            vs.Width = Webgame.Config.MapConfig.SupperVirusWidth;

            return true;
        }

        /// <summary>
        /// 让自己的病毒在下面3个回合内不被吞噬
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool 无敌(int playerID, int x, int y)
        {
            Webgame.Entity.VirusStatus vs = null;

            if (!VirusMap.TryGetValue(GetKey(x, y), out vs))
                return false;

            //  如果要求扩散的病毒不是发出命令的玩家，则不予考虑
            if (vs.pID != playerID)
                return false;

            Webgame.Entity.PlayerScore ps = GetPlayerScore(playerID);
            if (ps.Nutrient < 50)
                return false;   //  没有足够的养分

            ps.Nutrient -= 50;
            Webgame.Access.PlayerScore.Instance.Update(ps);

            vs.Status = new Webgame.Entity.ItemStatus();
            vs.Status.Status = Webgame.Entity.emVirusStatus.无敌;
            vs.Status.LessTime = 3;

            return true;
        }


        /// <summary>
        /// 让自己的病毒在下面3个回合内不被吞噬
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public bool 超级分裂(int playerID, int x, int y)
        {
            Webgame.Entity.VirusStatus vs = null;

            if (!VirusMap.TryGetValue(GetKey(x, y), out vs))
                return false;

            //  如果要求扩散的病毒不是发出命令的玩家，则不予考虑
            if (vs.pID != playerID)
                return false;

            Webgame.Entity.PlayerScore ps = GetPlayerScore(playerID);
            if (ps.Nutrient < 50)
                return false;   //  没有足够的养分

            ps.Nutrient -= 50;
            Webgame.Access.PlayerScore.Instance.Update(ps);

            vs.Status = new Webgame.Entity.ItemStatus();
            vs.Status.Status = Webgame.Entity.emVirusStatus.超级分裂;

            return true;
        }

        #endregion

    }
}
